package com.dapp;

import com.contracts.DogCoin;
import com.contracts.DogCoin2;
import com.service.DappService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.web3j.crypto.Credentials;
import org.web3j.protocol.core.methods.response.EthGasPrice;
import org.web3j.protocol.core.methods.response.TransactionReceipt;

import java.io.File;
import java.math.BigDecimal;
import java.math.BigInteger;

import static org.web3j.tx.Contract.GAS_LIMIT;
import static org.web3j.tx.ManagedTransaction.GAS_PRICE;

public class Test {
    private static final Logger log = LoggerFactory.getLogger(Test.class);

    public static void main(String[] args) throws Exception {
        log.info("started");
        Credentials credentials = DappService.loadCredentials(new File("G:\\sw\\Geth\\data\\keystore\\UTC--2018-03-30T10-05-01.202412500Z--0277f9a921ba3dcacf8d4ffbde713ab15b5afe3a"), "123456789");
        EthGasPrice ethGasPrice = DappService.web3j.ethGasPrice().send();
        log.info("eth gas price:" + ethGasPrice.getGasPrice());
//        DogCoin2 dogCoin = DogCoin2.deploy(DappService.web3j, credentials,
//                ethGasPrice.getGasPrice(), GAS_LIMIT,
//                new BigInteger("333333333"), "狗币2.3代", "￥", new BigInteger("2")
//        ).send();
        DogCoin2 dogCoin = DogCoin2.load("0x0bfE874f931C22b4B7ab3DD4F4ECEe2EA7Be1C26", DappService.web3j, credentials, ethGasPrice.getGasPrice(), GAS_LIMIT);
        log.info("contract deployed:" + dogCoin.getContractAddress());
        log.info("Value stored in remote smart contract:余额 " + dogCoin.name().send() + ":" + dogCoin.balanceOf(credentials.getAddress()).send());

        TransactionReceipt transactionReceipt = dogCoin.transfer("0x83A511b749341EeCD2Fd211682F7AE3a0ccb6Af7", new BigInteger("8888")).send();
        log.info("tranfer status" + transactionReceipt.getStatus());
        log.info("New value stored in remote smart contract:余额(创建者) " + dogCoin.name().send() + ":" + dogCoin.balanceOf(credentials.getAddress()).send());
        log.info("New value stored in remote smart contract:余额 " + dogCoin.name().send() + ":" + dogCoin.balanceOf("0x83A511b749341EeCD2Fd211682F7AE3a0ccb6Af7").send());
    }
}
